package com.lijie.bs_recruit.controller;


import com.lijie.bs_recruit.Util.JwtUtil;
import com.lijie.bs_recruit.VO.ResultCode;
import com.lijie.bs_recruit.VO.ResultVO;
import com.lijie.bs_recruit.model.Admin;
import com.lijie.bs_recruit.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李杰
 * @since 2021-03-15
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    //管理员登录
    @RequestMapping(value = "/login")
    public ResultVO adminlogin(@RequestBody Admin admin){
        Map<String, Object> loginResult = adminService.login(admin);
        //获得查询结果状态
        Integer status = (Integer)loginResult.get("status");
        ResultVO<Admin> resultVO = new ResultVO("登录",status);
        if(status==0){
            //登录成功
            Admin admin1 = (Admin) loginResult.get("admin");
            resultVO.setObject(admin1);
            String token = JwtUtil.sign(admin1.getAdminUsername());
            resultVO.setToken(token);
        }
        return resultVO;
    }
    //增加管理员
    @RequestMapping("/insertAdmin")
    public ResultVO insertAdmin(@RequestBody Admin admin){
        boolean save = adminService.save(admin);
        ResultVO<Admin> resultVO = new ResultVO();
        if(save){
            resultVO.setStatus(ResultCode.SUCCESS.getCode());
        }else{
            resultVO.setStatus(ResultCode.COMMON_FAIL.getCode());
        }
        return resultVO;
    }

    //根据id加载记录
    @RequestMapping("/selById/{id}")
    public Admin selById(@PathVariable Integer id){
        Admin byIdAdmin = adminService.getById(id);
        return byIdAdmin;
    }

    //修改管理员密码
    @RequestMapping("/updateAdminPassword")
    public ResultVO updateAdmin(@RequestBody Admin admin){
        boolean updateResult = adminService.updateById(admin);
        ResultVO<Admin> resultVO = new ResultVO();
        if(updateResult){
            resultVO.setStatus(ResultCode.SUCCESS.getCode());
        }else{
            resultVO.setStatus(ResultCode.COMMON_FAIL.getCode());
        }
        return resultVO;
    }






}

